package com.inossem.warehousestorage_inossem.activity.refund_storage;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.refundStorage.RefundStoragePickingListAdapter;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStoragePickingListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.refundStorage.RefundStoragePickingListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

public class RefundStoragePickingListActivity extends BaseActivity {

    TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView refundStorageList;

    private List<RefundStoragePickingListBean.OutPutBodyBean> allData;

    private RefundStoragePickingListAdapter adapter;

    private String mCondition;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_refund_storage_picking_list);
        initView(RefundStoragePickingListActivity.this);
        initData(RefundStoragePickingListActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        refundStorageList = (PullableListView) activity.findViewById(R.id.refundStorageList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.refundStorageListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(getResources().getString(R.string.activity_refund_storage_picking_list_list_of_receiving_restores));
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_main_title_short), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        refundStorageList.setPullUpFlag(false);
        refundStorageList.setPullDownFlag(false);
        mCondition = "";
        allData = new ArrayList<>();
        addPickingRefundListData(activity, allData, refundStorageList, true);
    }

    private void addPickingRefundListData(Context context, List<RefundStoragePickingListBean.OutPutBodyBean> saleOutStorageList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new RefundStoragePickingListAdapter(context, saleOutStorageList, mCondition, (String condition) -> {
                String checkResult = check(condition);
                if (checkResult == null) {
                    mCondition = condition;
                    getData(context, mCondition);
                } else {
                    Utils.middleToast(context, checkResult);
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private String check(String condition) {
        String result = null;
        if (TextUtils.isEmpty(condition)) {
            result = getResources().getString(R.string.activity_refund_storage_picking_list_the_query_condition_must_not_be_empty);
        }
        return result;
    }

    private void getData(Context context, String condition) {
        RefundStoragePickingListRequest.request(context, condition, new GsonBeanListener<RefundStoragePickingListBean>() {
            @Override
            public void onSuccess(RefundStoragePickingListBean bean) {
                allData = bean.getBody();
                if (allData.isEmpty()) {
                    Utils.middleToast(context, getResources().getString(R.string.activity_in_storage_receive_list_no_message));
                }
                addPickingRefundListData(context, ActivityUtils.getShowData(allData, 0), refundStorageList, true);
            }
        });
    }

    public interface RefundStorageManageListener {
        void onNoDoubleClickListener(String condition);
    }

}
